package kuzmenkov.applet;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Label;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * The component that shows an image with its name
 * @author Michael Kuzmenkov
 *
 */
public class TitledImageComp extends Container {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3385882257336850123L;
	
	/**
	 * The component that shows an image
	 */
	private ImageComp imageComp = new ImageComp();
	/**
	 * The component that shows a title of an image
	 */
	private Label titleLabel = new Label("nameless");
	
	/**
	 * Indicates to show of hide the border of this component
	 */
	private boolean border = false;

	/**
	 * The factor that is used for scalling an image showing by this component
	 */
	private float imageFactor = 0.7f;

	private GridBagConstraints gc1;

	public TitledImageComp(Image image, String title) {

		setImage(image);
		setTitle(title);

		createGUI();
		ScallingMouseListener l = new ScallingMouseListener();
		addMouseListener(l);
	}

	@Override
	public synchronized void addMouseListener(MouseListener l) {
		super.addMouseListener(l);
		titleLabel.addMouseListener(new TransMouseListener(this, l));
	}

	private void createGUI() {
		setLayout(new GridBagLayout());

		gc1 = new GridBagConstraints();
		gc1.anchor = GridBagConstraints.CENTER;
		gc1.fill = GridBagConstraints.NONE;
		gc1.gridheight = 1;
		gc1.gridwidth = 1;
		gc1.gridx = 0;
		gc1.gridy = 0;
		gc1.weighty = 100;
		gc1.weightx = 100;
		gc1.insets = new Insets(3, 3, 3, 3);

		add(imageComp, gc1);

		GridBagConstraints gc2 = new GridBagConstraints();
		gc2.anchor = GridBagConstraints.CENTER;
		gc2.fill = GridBagConstraints.NONE;
		gc2.gridwidth = 1;
		gc2.gridheight = 1;
		gc2.gridx = 0;
		gc2.gridy = 1;
		gc2.insets = new Insets(5, 5, 5, 5);
		gc2.weighty = 0;
		gc2.weightx = 0;

		add(titleLabel, gc2);

	}

	
	public void setImage(Image image) {
		this.imageComp.setImage(image);
	}

	public Image getImage() {
		return this.imageComp.getImage();

	}

	public String getTitle() {
		return this.titleLabel.getText();
	}

	public void setTitle(String title) {
		if (title == null)
			title = "";
		this.titleLabel.setText(title);
	}

	/**
	 * The class that listens mouse events of this component
	 * @author Michael Kuzmenkov
	 *
	 */
	private class ScallingMouseListener extends MouseAdapter {
		@Override
		public void mouseEntered(MouseEvent e) {
			gc1.fill = GridBagConstraints.BOTH;
			((GridBagLayout) getLayout()).setConstraints(imageComp, gc1);
			setBorder(true);
			TitledImageComp.this.doLayout();
		}

		@Override
		public void mouseExited(MouseEvent e) {
			gc1.fill = GridBagConstraints.NONE;
			((GridBagLayout) getLayout()).setConstraints(imageComp, gc1);
			setBorder(false);
			TitledImageComp.this.doLayout();
		}
	}

	@Override
	public void setPreferredSize(Dimension preferredSize) {
		super.setPreferredSize(preferredSize);
		Dimension d = new Dimension(
				(int) (preferredSize.getWidth() * imageFactor),
				(int) (preferredSize.getHeight() * imageFactor));
		this.imageComp.setPreferredSize(new Dimension(d));
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);
		if (isBorder())
			g.drawRect(1, 1, getWidth() - 2, getWidth() - 2);
	}

	public void setBorder(boolean border) {
		this.border = border;
		repaint();
	}

	public boolean isBorder() {
		return border;
	}

	public void setImageFactor(float imageFactor) {
		this.imageFactor = imageFactor;
	}

	public float getImageFactor() {
		return imageFactor;
	}

}
